package com.wanglibao.woodtrade.woodtrade.ui.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.entity.SupplyEntity;
import com.wanglibao.woodtrade.woodtrade.ui.activity.BuyActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ItemLvSupplyAndDemendAdapter extends BaseAdapter {

    private List<SupplyEntity.ListEntity> mList = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;
    boolean isok = false;

    public ItemLvSupplyAndDemendAdapter(Context context, List<SupplyEntity.ListEntity> list) {
        this.context = context;
        this.mList = list;
        this.layoutInflater = LayoutInflater.from(context);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    public void setList(List<SupplyEntity.ListEntity> list) {
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public SupplyEntity.ListEntity getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_lv_trade_fragment, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((SupplyEntity.ListEntity) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(final SupplyEntity.ListEntity object, ViewHolder holder) {
        holder.btItemLvGonghuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BuyActivity.class);
                intent.putExtra("ID", object.getId());
                context.startActivity(intent);
            }
        });
        holder.rlItemTradeFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BuyActivity.class);
                intent.putExtra("ID", object.getId());
                context.startActivity(intent);
            }
        });
        holder.tvItemLvQgTitle.setText(object.getTitle());
        holder.tvItemLvGuige.setText(object.getCategory_one_name());
        holder.tvItemLvQgSize.setText(object.getSize());
        holder.tvGhddLvQgGuige.setText(object.getBuy_address_province_name() + " | " + object.getBuy_address_city_name());
    }

    public void update(List<SupplyEntity.ListEntity> listEntities) {
        this.mList = listEntities;
        super.notifyDataSetChanged();
    }

    static class ViewHolder {
        @Bind(R.id.tv_item_lv_qg_title)
        TextView tvItemLvQgTitle;
        @Bind(R.id.tv_item_lv_size)
        TextView tvItemLvSize;
        @Bind(R.id.tv_item_lv_qg_size)
        TextView tvItemLvQgSize;
        @Bind(R.id.tv_item_lv_guige)
        TextView tvItemLvGuige;
        @Bind(R.id.bt_item_lv_gonghuo)
        Button btItemLvGonghuo;
        @Bind(R.id.tv_ghdd_lv_qg_guige)
        TextView tvGhddLvQgGuige;
        @Bind(R.id.rl_item_trade_fragment)
        RelativeLayout rlItemTradeFragment;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
